/*
 * @Author: Jone XIn
 * @Date: 2022-01-26 21:13:34
 * @LastEditTime: 2022-01-26 23:33:14
 * @Description: 项目节点事件全集
 */
const initProjectEvent = (graph) => {
    let timmer = null;
    loopRequestOnlineProject(timmer, graph);

    // cancel
    $("#project_cancel").click(() => {
        // 清空选项框
        clearAddExitServerDialog();
    });

    // confirm
    $("#project_confirm").click(async () => {
        const project_path = $("#join_projectPath").val().replaceAll(/\\/g, '\\');
        const ignore_list = $("#join_ignore").val();

        const res = await axios.post('http://127.0.0.1:4399/device/joinRoom', {
            data: {
                project_path,
                ignore_list,
                m_id: window.localStorage.getItem('mId'),
                sync_id: window.sessionStorage.getItem('c_sync_id')
            }
        })
        // if (res.data.code == 200) {
        //     clearAddExitServerDialog();
        //     // 增加此项目下的设备信息
        //     graph.addDevice(res.data.data.project_name, window.localStorage.getItem('mId'), res.data.data.sync_id);
        //     return;
        // }
        alert(res.data.message);
    });
}


function clearAddExitServerDialog() {
    $("#join_projectPath").val('');
    $("#join_ignore").val('');
    $("#join_project").hide();
}

// 轮询房间列表
function loopRequestOnlineProject(timmer, graph) {
    timmer && clearInterval(timmer);
    timmer = setInterval(async () => {
        let res = await axios.get('http://127.0.0.1:4399/device/getOnlineProject');
        if (res.data.code == 200) {
            // 刷新节点排布
            graph.refreshOnlineProjectList(res.data.data);
        } else {
            alert(res.data.code);
        }
    }, 5000)
}

export default initProjectEvent;
